package com.problem.leetcode;

public class Problem4 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] result = new int[nums1.length + nums2.length];
        int   begin1 = 0;
        int   begin2 = 0;
        int   begin3 = 0;
        int   end1   = nums1.length;
        int   end2   = nums2.length;

        while (begin1 < end1 && begin2 < end2) {
            if (nums1[begin1] < nums2[begin2]) {
                result[begin3++] = nums1[begin1++];
            } else if (nums1[begin1] >= nums2[begin2]) {
                result[begin3++] = nums2[begin2++];
            }
        }

        while (begin1 < end1) {
            result[begin3++] = nums1[begin1++];
        }
        while (begin2 < end2) {
            result[begin3++] = nums2[begin2++];
        }

        return result.length % 2 == 0 ?
               (result[result.length / 2 - 1] + result[result.length / 2]) / 2.0 :
               result[result.length / 2];

    }

    public static void main(String[] args) {
        // double result = new Problem4().findMedianSortedArrays(new int[]{1, 2, 3}, new int[]{3, 4});
        // System.out.println(result);
        System.out.println((2 +3) / 2.0);
    }

}
